package org.vz.product.vserv.pf.web;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vz.product.vserv.pf.domain.entity.DruingTimeEntity;
import org.vz.product.vserv.pf.domain.result.BaseResult;
import org.vz.product.vserv.pf.service.ReceivedSerCountService;
import org.vz.product.vserv.pf.service.ReceivedSerExceptionAndWaitCountService;
import org.vz.product.vserv.pf.service.ReceivedSerOvershootCountService;
import org.vz.product.vserv.pf.service.ReceivedSerOverviewService;
import org.vz.product.vserv.pf.service.ReceivedSerParticularsService;
import org.vz.product.vserv.pf.utils.StringUtil;

@Controller
@RequestMapping("/pf")
public class SerOperations {
	@Autowired
	ReceivedSerCountService receivedSerCountService;
	@Autowired
	ReceivedSerExceptionAndWaitCountService receivedSerExceptionAndWaitCountService;
	@Autowired
	ReceivedSerOvershootCountService receivedSerOvershootCountService;
	@Autowired
	ReceivedSerOverviewService receivedSerOverviewService;
	@Autowired
	ReceivedSerParticularsService receivedSerParticularsService;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 获取标准服务使用量
	 * 
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param type
	 *            时间定义类型
	 * @return
	 */
	@RequestMapping("/getServiceUseCount")
	@ResponseBody
	private String getServiceUseCount(
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime,
			@RequestParam(value = "type", defaultValue = "0") int type) {

		DruingTimeEntity dtn = new DruingTimeEntity();
		dtn.endTime = endTime;
		dtn.startTime = startTime;
		dtn.status = type;
		dtn = setTime(dtn);
		BaseResult br = new BaseResult();
		if (dtn.status == -1) {
			br.msg = "自定时间不能为空！！！";
			return StringUtil.toJsonString(br);
		} else if (dtn.status == -2) {
			br.msg = "自定时间不能为空！！！";
			return StringUtil.toJsonString(br);
		} else {
			return StringUtil.toJsonString(receivedSerCountService.getSerCount(
					dtn.startTime, dtn.endTime));
		}
	}

	/**
	 * 获取标准服务异常待处理数量
	 * 
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param type
	 *            时间定义类型
	 * @return
	 */
	@RequestMapping("/getServiceExceptionAndWaitCount")
	@ResponseBody
	private String getServiceExceptionAndWaitCount(
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime,
			@RequestParam(value = "type", defaultValue = "0") int type) {

		DruingTimeEntity dtn = new DruingTimeEntity();
		dtn.endTime = endTime;
		dtn.startTime = startTime;
		dtn.status = type;
		dtn = setTime(dtn);
		BaseResult br = new BaseResult();
		if (dtn.status == -1) {
			br.msg = "自定时间不能为空！！！";
			return StringUtil.toJsonString(br);
		} else if (dtn.status == -2) {
			br.msg = "自定时间不能为空！！！";
			return StringUtil.toJsonString(br);
		} else {
			return StringUtil
					.toJsonString(receivedSerExceptionAndWaitCountService
							.receivedSerExceptionAndWaitCountService(
									dtn.startTime, dtn.endTime));
		}
	}

	/**
	 * 获取最后一个时间段标准服务的超负载数及比例
	 * 
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param overShoot
	 *            超标的量 一般在乘以0.8
	 * @return
	 */
	@RequestMapping("/getSerOvershootCountService")
	@ResponseBody
	private String getSerOvershootCountService(
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime,
			@RequestParam(value = "overShoot", defaultValue = "0") int overShoot) {
		Calendar c = Calendar.getInstance();
		if (startTime.equals("") || endTime.equals("")) {
			startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60 * 1);
			endTime = sdf.format(c.getTimeInMillis());
		}
		if (overShoot <= 0) {
			overShoot = 20;
		}
		return StringUtil
				.toJsonString(receivedSerOvershootCountService
						.receivedSerOvershootCountService(startTime, endTime,
								overShoot));

	}

	/**
	 * 获取最后一个时间段标准服务的超负载数及比例
	 * 
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param overShoot
	 *            超标的量 一般在乘以0.8
	 * @return
	 */
	@RequestMapping("/getSerOverviewService")
	@ResponseBody
	private String getSerOverviewService(
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime,
			@RequestParam(value = "overShoot", defaultValue = "0") int overShoot) {
		Calendar c = Calendar.getInstance();
		if (startTime.equals("") || endTime.equals("")) {
			startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60 * 1);
			endTime = sdf.format(c.getTimeInMillis());
		}
		if (overShoot <= 0) {
			overShoot = 20;
		}
		return StringUtil.toJsonString(receivedSerOverviewService
				.receivedSerOverviewService(startTime, endTime, overShoot));

	}

	/**
	 * 获取最后一个时间段标准服务的超负载数及比例
	 * 
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param overShoot
	 *            超标的量 一般在乘以0.8
	 * @return
	 */
	@RequestMapping("/getSerParticularsService")
	@ResponseBody
	private String getSerParticularsService(
			@RequestParam(value = "type", defaultValue = "0") int type) {
		return StringUtil.toJsonString(receivedSerParticularsService
				.receivedSerParticularsService(type));

	}

	/**
	 * 定义时间
	 * 
	 * @param druingTimeEntity
	 * @return
	 */
	private DruingTimeEntity setTime(DruingTimeEntity druingTimeEntity) {
		DruingTimeEntity dtn = new DruingTimeEntity();
		Calendar c = Calendar.getInstance();
		switch (druingTimeEntity.status) {
		case 0:// 近一小时
			dtn.startTime = sdf
					.format(c.getTimeInMillis() - 1000 * 60 * 60 * 1);
			dtn.endTime = sdf.format(c.getTimeInMillis());
			dtn.status = 1;
			break;
		case 1:// 近一天
			dtn.startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60
					* 24);
			dtn.endTime = sdf.format(c.getTimeInMillis());
			dtn.status = 1;
			break;
		case 2:// 近一周
			dtn.startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60
					* 24 * 7);
			dtn.endTime = sdf.format(c.getTimeInMillis());
			dtn.status = 1;
			break;
		case 3:// 近一个月
			dtn.startTime = sdf.format(c.getTimeInMillis() - 1000 * 60 * 60
					* 24 * 30);
			dtn.endTime = sdf.format(c.getTimeInMillis());
			dtn.status = 1;
			break;
		case 4:// 自定义
			if (druingTimeEntity.startTime.equals("")
					|| druingTimeEntity.endTime.equals("")) {
				dtn.status = -2;
			}
			break;
		default:
			break;
		}
		return dtn;
	}
}
